// @ts-nocheck
import http from '@ohos.net.http';
import prompt from '@system.prompt';
import citys from '../cityId.js';

export default {
    props: {
        cityBean: {},
    },
    data: {
    //        loading:false,
        contentShow:false,
        fromCity: '出发地',
        toCity: '目的地',
        fromCityCode: '',
        toCityCode: '',
        textValue: 'default',
        textValue1: 'default',
        selectProvince: '',
        selectCity: '',
        selectProvince2: '',
        selectCity2: '',
        provinceBeans: [['北京', '上海', '重庆', '天津'], ['北京市', '上海市', '重庆市', '天津市']],
        cityBeans: [['北京', '上海', '重庆', '天津'], ['北京市', '上海市', '重庆市', '天津市']],
        fromOutDesc: '', //出出发地信息
        fromHighInDesc: '', //进出发地信息
        toOutDesc: '', //出目的地信息
        toHighInDesc: '', //进目的地信息
        risk:'',
        risk2:'',
        queryResult: {},
        form:{
            key1:'cd30748cd26060fb22b2193936a99280'
        }
    },
    onInit() {
        this.$watch('cityBean', 'setCity');
        this.setCity();

    },
    setCity() {
        console.error('TAG---获取城市Bean：' + JSON.stringify(this.cityBean));
        //        StringEscapeUtils.unescapeJavaScript(this.cityBean)

        //设置省
        var arr = [];
        var arr2 = [];
        for (let index in citys) {
            arr.push(citys[index].label)
        }
        for(let index in citys[0].children){
            arr2.push(citys[0].children[index].label)
        }
        this.cityBeans[0] = arr;
        this.cityBeans[1] = arr2;

    },

//显示弹窗
    showDialog() {
        this.$element('selectDialog').show();
    },
    cancelDialog() {
        this.$element('selectDialog').cancel();
    },
//显示弹窗
    showDialog2() {
        this.$element('selectDialog2').show();
    },
    cancelDialog2() {
        this.$element('selectDialog2').cancel();
    },

    //切换
    switchCity() {
        var temp = this.fromCity;
        this.fromCity = this.toCity;
        this.toCity = temp;

        var temp1 = this.fromCityCode;
        this.fromCityCode = this.toCityCode;
        this.toCityCode = temp1;
        //        this.loading = false;

        if (!this.isEmpty(this.fromCityCode) && !this.isEmpty(this.toCityCode)) {
            this.queryData(this.fromCityCode, this.toCityCode);
        }
    },
    //监听picker列变化
    oncolumnchange(index,column,data) {
//        console.error("TAG---oncolumnchange--" +"index--"+index+'--column--'+column+ JSON.stringify(data));

        if (index == 1) {
            this.selectProvince = data.newSelected;
            console.error('TAG---列变化1111'+JSON.stringify(data));
//            console.error('TAG---列变化：' + JSON.stringify(citys[this.selectProvince].children));
            if (data.column == 0) {
                this.setFromCity(data.newValue);
            }
        }else if (index == 2) {
            this.selectProvince2 = data.newSelected;
            if (data.column == 0) {
                this.setToCity(data.newValue);
            }
        }
    },
    //picker最终结果
    onchange(index, select) {
        var code;
        this.selectCity = select.newValue[1];
        this.selectProvince = select.newSelected;
        prompt.showToast({
            message: "选择省：" + this.selectCity
        })
        console.error('TAG---'+this.selectCity)
        for(let index in citys){
            if (citys[index].label == select.newValue[0]) {
                for(let index1 in citys[index].children){
                    if (citys[index].children[index1].label == this.selectCity) {
                        code = citys[index].children[index1].value;
                    }
                }
            }
        }

        if (code.length == 7) {
            code = code.substring(0,5);
        }

        if (index == 1) {
            this.fromCity = this.selectCity;
            this.fromCityCode = code;
        } else if (index == 2) {
            this.toCity = this.selectCity;
            this.toCityCode = code;
        }

        if (!this.isEmpty(this.fromCityCode) && !this.isEmpty(this.toCityCode)) {
            this.queryData(this.fromCityCode, this.toCityCode);
        }
    },
    //设置出发地
    setFromCity(select){

        var total = [];
        //设置省
        var arr = [];
        //设置市
        var arr2 = [];
        for (let index in citys) {
            arr.push(citys[index].label)
            if (select == citys[index].label) {
                for(let index in citys[this.selectProvince].children){
                    arr2.push(citys[this.selectProvince].children[index].label)
                }
            }
        }

        total[0] = arr;
        total[1] = arr2;
        this.cityBeans = total;
    },
    //设置目的地
    setToCity(select){
        var arr3 = [];

        var total1 = [];
        //设置省
        var arr1 = [];
        for (let index in citys) {
            arr1.push(citys[index].label)
        }

        //设置市
        for(let index in citys[this.selectProvince2].children){

            arr3.push(citys[this.selectProvince2].children[index].label)
        }
        total1[0] = arr1;
        total1[1] = arr3;
        this.cityBeans = total1;
    },
    oncancel() {
        prompt.showToast({
            message: "multitextoncancel"
        })
    },
    queryData(fromId, toId) {
        this.url = `http://apis.juhe.cn/springTravel/query?key=${this.form.key1}&from=${fromId}&to=${toId}`;
        let httpRequest = http.createHttp();
        httpRequest.request(this.url, {
            method: 'GET',
        }, (err, data) => {
            if (err == null) {

                this.queryResult = JSON.parse(data.result);
                console.log('TAG---' + this.queryResult);
                this.fromOutDesc = this.queryResult.result.from_info.out_desc;
                this.fromHighInDesc = this.queryResult.result.from_info.high_in_desc;
                this.toOutDesc = this.queryResult.result.to_info.out_desc;
                this.toHighInDesc = this.queryResult.result.to_info.high_in_desc;
//                0:暂无 1:低风险 2:中风险 3:高风险 4:部分地区中风险 5:部分地区高风险 6:部分地区中、高风险
                var level1 = this.queryResult.result.from_info.risk_level;
                var level2 = this.queryResult.result.to_info.risk_level;
                if(level1 == 0 || level2 == 0){
                    this.risk = '暂无';
                    this.risk2 = '暂无';
                }else if(level1 == 1 || level2 == 1){
                    this.risk = '低风险';
                    this.risk2 = '低风险';
                } else if(level1 == 2 || level2 == 2){
                    this.risk = '中风险';
                    this.risk2 = '中风险';
                } else if(level1 == 3 || level2 == 3){
                    this.risk = '高风险';
                    this.risk2 = '高风险';
                } else if(level1 == 4 || level2 == 4){
                    this.risk = '部分地区中风险';
                    this.risk2 = '部分地区中风险';
                } else if(level1 == 5 || level2 == 5){
                    this.risk = '部分地区高风险';
                    this.risk2 = '部分地区高风险';
                } else if(level1 == 5 || level2 == 5){
                    this.risk = '部分地区中、高风险';
                    this.risk2 = '部分地区中、高风险';
                }
                this.contentShow = true;


            } else {
                console.error('TAG---error' + JSON.stringify(err));
                prompt.showToast({
                    message: '请求失败，请稍后再试！'
                })
            }
        });
    },

    //判断字符是否为空的方法
    isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == ""){
        return true;
    }else{
        return false;
    }
}
}